package com.kobako.model;

import com.kobako.entity.EssayEntity;
import com.kobako.mybatis.MySFFactory;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.Pack200;

/**
 * Created by kobako on 2017/4/11.
 * Just a game
 */
@Service
public class EssayDao {
    private SqlSession openSession() {
        return MySFFactory.getSessionFactory().openSession();
    }

    public List<EssayEntity> getAllTitles() {
        String stat = "com.kobako.mybatis.essayMaspper.getEssayTitles";
        List<EssayEntity> essays = getAll(stat);
        return essays;
    }

    public List<EssayEntity> getAllBlogs() {
        String stat = "com.kobako.mybatis.essayMaspper.getBlogTitles";
        List<EssayEntity> blogs = getAll(stat);
        return blogs;
    }

    private List<EssayEntity> getAll(String stat) {
        List<EssayEntity> essays = null;
        SqlSession session = openSession();
        try {
            essays = session.selectList(stat);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return essays;
    }

    public EssayEntity getEssayById(int id) {
        String stat = "com.kobako.mybatis.essayMaspper.getEssayById";
        EssayEntity essay = getById(stat,id);
        return essay;
    }

    public EssayEntity getBlogById(int id) {
        String stat = "com.kobako.mybatis.essayMaspper.getBlogById";
        EssayEntity essay = getById(stat,id);
        return essay;
    }

    private EssayEntity getById(String stat,int id){
        EssayEntity essay = null;
        SqlSession session = openSession();
        try {
            essay = session.selectOne(stat, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return essay;
    }

    public void insertEssay(EssayEntity essay) {
        String stat = "com.kobako.mybatis.essayMaspper.insertEssay";
        insertIt(stat, essay);
    }

    public void insertBlog(EssayEntity essay) {
        String stat = "com.kobako.mybatis.essayMaspper.insertBlog";
        insertIt(stat, essay);
    }

    private void insertIt(String stat, EssayEntity essay) {
        SqlSession session = openSession();
        try {
            session.insert(stat, essay);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

//    @Test
//    public void testGetAllTitles() {
//        EssayEntity essay = getEssayById(1);
//        System.out.println(essay);
//    }
//
//    @Test
//    public void testInsertEssay() {
//        EssayEntity essay = new EssayEntity();
//        for(int i=0;i<10;i++){
//            essay.setTitle("test blog"+i);
//            essay.setContent("Yes I am!");
//            insertBlog(essay);
//        }
//    }
}
